import 'package:WebTest/sharedData/viewmodel/counter_view_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  /*
    * 1.先创建自己要共享的数据
    * 2.在我们应用程序的顶层ChangeNotifierProvider
    * 3.在其他位置使用共享数据
    * > Provider.of :当Provider中的数据发生改变时，Provider.of所在的Widget这个那个build方法都会重新构建
    * > Consumer(相对推荐):当Provider中的数据发生改变时，重新执行Consumer的builder方法
    * > Selector 和 Consumer的区别是selector能减少builder方法重新构建widget，而Consumer是能减少引用Provider.of方法的build重新构建widget造成过多开销
    * 如果是仅是用来引用共享数据，比如floatingActionButton就可以用Selector，然而如果在引用的同时刷新页面就要用Consumer
    * 4.Selector
    * 4.1对原有的数据进行转换
    * 4.2shouldRebuild要不要重新构建*/

//  runApp(ChangeNotifierProvider(
//    child: MyApp(),
//    create: (BuildContext context) => XJCounterViewModel(),
//  ));
//  多个共享数据的用法
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(
        create: (BuildContext context) => XJCounterViewModel(),
      ),
    ],
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: HomePage(),
        ),
        floatingActionButton: Selector<XJCounterViewModel, XJCounterViewModel>(
          selector: (context, viewModel) => viewModel,
//         shouldRebuild 要不要重新构建
          shouldRebuild: (prev, next) => false,
          builder: (context, viewModel, child) {
            print("floatingActionButton的Consumer的builder方法");
            return FloatingActionButton(
                child: child,
                onPressed: () {
                  viewModel.counter++;
                });
          },
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        HYShowData01(),
        HYShowData02(),
      ],
    );
  }
}

class HYShowData01 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("HYShowData01的build方法");
    int counter = Provider.of<XJCounterViewModel>(context).counter;
    return Container(
      child: Text(
        "当前计数：$counter",
        style: TextStyle(fontSize: 30),
      ),
      color: Colors.lightBlue,
    );
  }
}

class HYShowData02 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("HYShowData02的build方法");
    return Container(
      child: Consumer<XJCounterViewModel>(
        builder: (context, viewModel, child) {
          print("HYShowData02的Consumer的builder方法");
          return Text(
            "当前计数：${viewModel.counter}",
            style: TextStyle(fontSize: 30),
          );
        },
      ),
      color: Colors.red,
    );
  }
}
